package com.seqb.manage.dict.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.dict.domain.DictDO;
import com.seqb.manage.dict.service.DictService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 字典表
 * @description 请填写相关描述
 * @date 2019/11/15
 */
@Controller
@RequestMapping("/manage/dict/dict")
public class DictController extends BaseController {
    @Autowired
    private DictService dictService;

    @GetMapping()
    @RequiresPermissions("manage:dict:dict:list")
    @Log("字典表列表")
    String dict() {
        return "manage/dict/dict.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:dict:dict:list")
    @Log("字典表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<DictDO> page = dictService.list(query);
        return new PageUtils(page.getResult(), page.getTotal());
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:dict:dict:add")
    @Log("字典表添加")
    String add(Model model) {
        model.addAttribute("parentList", dictService.list(null));
        return "manage/dict/dict.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:dict:dict:edit")
    @Log("字典表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        DictDO dict = dictService.get(id);
        model.addAttribute("parentList", dictService.list(null));
        model.addAttribute("dict", dict);
        return "manage/dict/dict.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:dict:dict:add")
    @Log("字典表添加保存")
    public ResultVo save(DictDO dict) {
        return dictService.save(dict);
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("字典表编辑保存")
    @RequiresPermissions("manage:dict:dict:edit")
    public ResultVo update(DictDO dict) {
        dictService.update(dict);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:dict:dict:remove")
    @Log("字典表删除")
    public ResultVo remove(Long id) {
        if (dictService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:dict:dict:remove")
    @Log("字典表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        dictService.batchRemove(ids);
        return ResultVo.ok();
    }

    /**
     * 数据同步到缓存
     */
    @PostMapping("/syncDictCache")
    @ResponseBody
    @RequiresPermissions("manage:dict:dict:sync")
    @Log("字典表数据同步到缓存")
    public ResultVo syncDictCache() {
        return dictService.syncDictCache();
    }

    /**
     * 获取字典数据
     */
    @ResponseBody
    @RequestMapping("/getDictList")
    @Log("获取字典表数据")
    public PageUtils getDictList(String type, Long parentId) {
        List<DictDO> dictList = dictService.getDictList(type, parentId);
        return new PageUtils(dictList, dictList.size());
    }
}
